Palindrome Linked List || Valid Palindrome || Palindrome Partitioning

Palindrome Linked List

Question

Given a singly linked list, determine if it is a palindrome.

Analysis

判断一个链表是否为回文数

  • 判断链表节点个数是奇数还是偶数
  • 偶数的情况下,两个指针,p1一次走一步,并将路过的元素都压栈,p2一次走两步,当p2为null时,p1刚好指向链表中点,此时p1接着向下,元素与依次弹栈的元素进行对比,假如不等,则返回false,否则返回true
  • 奇数的情况下,先将p2先后挪一位,其余相同
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isPalindrome(ListNode head) {
ListNode onestep=head;
ListNode twostep=head;
ListNode tmp=head;
int size=0;
while(tmp!=null){
size++;
tmp=tmp.next;
}
boolean result=Findmidnode(onestep,twostep,size);
return result;
}
boolean Findmidnode(ListNode tmp1,ListNode tmp2,int size){
Stack<Integer> stack=new Stack();
if(size%2==0){
while(tmp2!=null){
stack.push(tmp1.val);
tmp1=tmp1.next;
tmp2=tmp2.next.next;
}
}else{
tmp2=tmp2.next;
while(tmp2!=null){
stack.push(tmp1.val);
tmp1=tmp1.next;
tmp2=tmp2.next.next;
}
tmp1=tmp1.next;
}
while(stack.isEmpty()!=true){
if(stack.pop()!=tmp1.val)
return false;
tmp1=tmp1.next;
}
return true;
}
}

Valid Palindrome

Question

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

Analysis

利用StringBuilder将所有符合条件的字符复制至salpha,之后判断salpha大小,利用toUpperCase将所有的字符转换为大写后判断字符是否相等。

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Solution {
public boolean isPalindrome(String s) {
StringBuilder salpha=new StringBuilder();
for(int i=0;i<s.length();i++){
if((s.charAt(i)>='a'&&s.charAt(i)<='z')||(s.charAt(i)>='A'&&s.charAt(i)<='Z')||(s.charAt(i)>='0'&&s.charAt(i)<='9'))
salpha.append(s.charAt(i));
}
String str=salpha.toString();
int size_str=str.length();
String strupper=str.toUpperCase();
for(int j=0;j<size_str/2;j++){
if(strupper.charAt(j)!=strupper.charAt(size_str-j-1))
return false;
}
return true;
}
}

Palindrome Partitioning

Question

Given a string s, partition s such that every substring of the partition is a palindrome.

Return all possible palindrome partitioning of s.

For example, given s = “aab”,
Return

1
2
3
4
[
["aa","b"],
["a","a","b"]
]
Analysis

DFS

  • 从头开始遍历字符串,利用i循环截取不同长度的子串,当截取字符串为回文串时将其加入item,以当前为起点进入下一次调用。
  • 当start等于字符串长度的时候,将item加入result,并且调用结束后逐步将item清空。
  • List初始化方法:List> res = new ArrayList<>()
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Solution {
public ArrayList<ArrayList<String>> partition(String s) {
ArrayList<String> item = new ArrayList<String>();
List<List<String>> res = new ArrayList<>();
if(s==null||s.length()==0)
return res;
dfs(s,0,item,res);
return res;
}
public void dfs(String s, int start, ArrayList<String> item, ArrayList<ArrayList<String>> res){
if (start == s.length()){
res.add(new ArrayList<String>(item));
return;
}
for (int i = start; i < s.length(); i++) {
String str = s.substring(start, i+1);
if (isPalindrome(str)) {
item.add(str);
dfs(s, i+1, item, res);
item.remove(item.size() - 1);
}
}
}
public boolean isPalindrome(String s){
int low = 0;
int high = s.length()-1;
while(low < high){
if(s.charAt(low) != s.charAt(high))
return false;
low++;
high--;
}
return true;
}
}